--Redzone RPG
--By Kylegar


rzItem = {}

ItemClasses = {}
ItemsActive = {}
--[[
NON META FUNCTIONS
These are for creating and finding Items
--]]

function rzItem.RegisterItem(item)	
	table.insert(ItemClasses,item)
end


function rzItem.Create(classname)
	for k,v in pairs(ItemClasses) do
		if (table.HasValue(v, classname)) then
			local newitem = table.Copy(v)
			setmetatable(newitem , self)
			self.__index = self
			newitem.ItemID = #ItemsActive + 1
			table.insert(ItemsActive , newitem)
			return 	newitem
		end
	end
end

function ShowItemClasses()
Msg("Items: \n")
	for k,v in pairs(ItemClasses) do
		Msg("  "..k.." "..type(v).."\n")
		for e,r in pairs(v) do
			Msg("    "..e.." ["..type(r).."] "..tostring(r).."\n")
				
		end
	end
end

function rzItem.FindAll()
	return ItemsActive
end


function rzItem.FindByClass(classname)
	local ItemBuff = {}
	for k,v in pairs(ItemsActive) do
		if(table.HasValue(v , classname)) then
			table.insert(ItemBuff , v)
		end
		
	end
	return ItemBuff
end


function rzItem.FindByItemID(itemID)
	local ItemBuff = {}
	for k,v in pairs(ItemsActive) do
		if(table.HasValue(v , itemID)) then
			table.insert(ItemBuff , v)
		end	
	end
	return ItemBuff		
end

function rzItem.FindItemsOnGround()
	local ItemBuff = {}
	for k,v in pairs(ItemsActive) do
		if(v:IsOnGround()) then
			table.insert(ItemBuff , v)
		end
	end
	return ItemBuff	
end

--[[ 
META FUNCTIONS
]]--

function rzItem:GetClassName()
	return self.Classname
end

function rzItem:GetName()
	return self.Printname
end


function rzItem:GetItemID()
	return self.ItemID
end

function rzItem:SetItemOwner(ply)
	self.Owner = ply
end

function rzItem:GetOwner()
	return self.Owner
end

function rzItem:IsOnGround()
	return self.OnGround
end

function rzItem:SetModel(modelpath)
	self.Model = modelpath
end

function rzItem:IsValid()
	if !(self.IsItem) then return false end
	
	if (table.HasValue(ItemsActive, self)) then
		for k , v in pairs(ItemsActive) do
			if (table.HasValue(v , self.ItemID)) then
				return true
			end
		end
	end			
end

function rzItem:Remove()
	if(self.Owner && self.Owner:IsPlayer()) then
		if(self.Ammount > 1) then
			self.Ammount = self.Ammount - 1
		else
			self.Owner:RemoveFromInv(self)
		end
		return
	end
	if(table.HasValue(ItemsActive, self)) then
		for k,v in pairs(ItemsActive) do
			if(table.HasValue(v, self.ItemID)) then
				table.remove(ItemsActive , k)
				return
			end
		end
	end
	
end 


function rzItem:Use()
	self.OnUse()
end
